<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2262 – This alias can't be defined and used in the same parsing unit. Use a function instead.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2262 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2262">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="this-alias-cant-be-defined-and-used-in-the-same-parsing-unit-use-a-function-instead">This
alias can't be defined and used in the same parsing unit. Use a function
instead.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2262.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span><span class="fu"> checksum()</span> <span class="kw">{</span></span>
<span id="cb1-2"><a href="SC2262.html#cb1-2" aria-hidden="true" tabindex="-1"></a>  <span class="bu">type</span> md5 <span class="kw">&amp;&amp;</span> <span class="bu">alias</span> md5sum=md5</span>
<span id="cb1-3"><a href="SC2262.html#cb1-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">md5sum</span> <span class="st">&quot;</span><span class="va">$@</span><span class="st">&quot;</span>  <span class="co"># This calls `md5sum`, not `md5`</span></span>
<span id="cb1-4"><a href="SC2262.html#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2262.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span><span class="fu"> checksum()</span> <span class="kw">{</span></span>
<span id="cb2-2"><a href="SC2262.html#cb2-2" aria-hidden="true" tabindex="-1"></a>  <span class="bu">type</span> md5 <span class="kw">&amp;&amp;</span> <span class="fu">md5sum()</span> <span class="kw">{</span> <span class="ex">md5</span> <span class="st">&quot;</span><span class="va">$@</span><span class="st">&quot;</span><span class="kw">;</span> <span class="kw">}</span></span>
<span id="cb2-3"><a href="SC2262.html#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">md5sum</span> <span class="st">&quot;</span><span class="va">$@</span><span class="st">&quot;</span>  <span class="co"># Now this would call `md5` when applicable</span></span>
<span id="cb2-4"><a href="SC2262.html#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>Alias expansion happens at parse time, which means to have an effect,
the <code>alias</code> command must be executed not just before the
alias is invoked, but before the invocation is parsed.</p>
<p>A shell will parse commands until it has a complete set of commands
followed by a linefeed. This includes compound commands like
<code>{ brace; groups; }</code> and
<code>while loops; do true; done</code>. Here are some examples:</p>
<pre><code># A single command followed by a linefeed is one unit
unit 1

# These commands are in the same parsing unit because
# there is no line feed between them
unit 2; unit 2;

# These commands are in the same parsing unit because
# they are part of the same top level brace group
{
  unit 3
  unit 3
}

# These commands are in the same parsing unit because
# there is no linefeed between the groups.
{
  unit 4
}; {
  unit 4
}</code></pre>
<p>Any alias defined in a command in <code>unit 1</code> would not take
effect until <code>unit 2</code> and beyond. Similarly, an alias defined
in unit 2 will only take effect in unit 3 and 4.</p>
<p>In the problematic example, the alias is defined and used in a
function. Since a function definition is a single compound command, it's
considered a single parsing unit. The alias would therefore not have an
effect (this is true even if the function is invoked twice, because it's
only parsed once).</p>
<p>Does this sound confusing and counter-intuitive? It is. Save yourself
the trouble and always use functions instead of aliases.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>If the flagged commands are not expected to use the alias, you can
ignore this error. ShellCheck may incorrectly flag this if the alias
definition and usage were in different branches of an <code>if</code>
statement.</p>
<p>You can <a href="ignore.html">ignore</a> this warning with a directive.
All warnings may always be disabled either before the relevant command
or before any outer compound commands, but in this case it's especially
useful:</p>
<pre><code># shellcheck disable=SC2262    # Option A, before compound command
if true
then
  # shellcheck disable=SC2262  # Option B, before alias command
  alias foo=bar
  # With either Option A or B, this SC2263 message is auto-suppressed
  foo
fi</code></pre>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li>Unix&amp;Linux SE: <a
href="https://unix.stackexchange.com/questions/502257/bash-aliases-do-not-expand-even-with-shopt-expand-aliases">bash
aliases do not expand even with shopt expand_aliases</a></li>
<li>Help by adding links to BashFAQ, StackOverflow, man pages, POSIX,
etc!</li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


